package com.glsc.ngateway.common.base.utils;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author zhengyr
 * @date 2023/8/4-9:03
 * 增加如果是空List后抛错的链式调用
 */
public class CollectionBuilder<T> {
    private Collection<T> collection;
    private CollectionBuilder(Collection<T> collection) {
        this.collection = collection;
    }
    private CollectionBuilder() {
        this.collection = null;
    }
    public static <T> CollectionBuilder notEmpty(Collection<T> collection) {
        if(collection == null || collection.isEmpty()){
            return new CollectionBuilder();
        }
        return new CollectionBuilder(collection) ;
    }
    public <X extends Exception> Collection<T> orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
        if (collection == null || collection.isEmpty()) {
            throw exceptionSupplier.get();
        } else {
            return collection;
        }
    }

    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null || collection.isEmpty());
    }
    public static boolean isEmpty(Map<?, ?> map) {
        return (map == null || map.isEmpty());
    }
}
